package com.design.work322;

public class ChinesePostalCode {

    /**
     * 判断中国邮编是否合法 6位数字
     * @param postalCode
     * @param province
     * @return
     */
    public boolean isValidChinesePcode(String postalCode, String province) {
        char[] pCode = toNonBlankCharArray(postalCode);

        // 检查长度是否为6
        if (pCode.length != 6) {
            return false;
        }

        // 检查每个字符是否为数字
        for (char c : pCode) {
            if (!Character.isDigit(c)) {
                return false;
            }
        }

        return true;
    }

    // 去除空格的方法保持不变
    public char[] toNonBlankCharArray(String postalCode) {
        int m = 0;

        for (int k = 0; k < postalCode.length(); k++) {
            if (!Character.isSpaceChar(postalCode.charAt(k))) {
                m++;
            }
        }

        char[] p = new char[m];

        int n = 0;
        for (int k = 0; k < postalCode.length(); k++) {
            if (!Character.isSpaceChar(postalCode.charAt(k))) {
                p[n] = postalCode.charAt(k);
                n++;
            }
        }
        return p;
    }
}
